var addnewcontact = (function (e){
    var initials = false, cID, conID;

    e.initAddNewContact = function(clientID){
        cID = clientID;
        conID = 0;
        e.init();
    };
    e.editContact = function(contactID, clientID){
        conID = contactID;
        cID = clientID;
        dataBase.getClientContactByID(conID, function(data){
            $("#selSalutation option[value='" + data.Salutation + "']").attr('selected', 'selected');
            $('#selSalutation').selectmenu('refresh', true);
            $("#txtContactName").val(data.Name);
            $("#txtCompanyPosition").val(data.Position);
            $("#txtEmailContact1").val(data.Email1);
            $("#txtEmailContact2").val(data.Email2);
            $("#txtPhoneContact1").val(data.Phone1);
            $("#txtPhoneContact2").val(data.Phone2);
            $("#txtPhoneContact3").val(data.Phone3);
            $("#txtPhoneContact4").val(data.Phone4);
            $("#txtFaxContact").val(data.Fax);
            $("#txtContactNote").val(data.Note);
        });
        e.init();
    };
    e.resetContact = function (){

        var temp = $("#selSalutation option").eq(0).val();
        $("#selSalutation option[value='" + temp + "']").attr('selected', 'selected');
        $('#selSalutation').selectmenu('refresh', true);
        $("#txtContactName").val("");
        $("#txtCompanyPosition").val("");
        $("#txtEmailContact1").val("");
        $("#txtEmailContact2").val("");
        $("#txtPhoneContact1").val("");
        $("#txtPhoneContact2").val("");
        $("#txtPhoneContact3").val("");
        $("#txtPhoneContact4").val("");
        $("#txtFaxContact").val("");
        $("#txtContactNote").val("");
    };
    e.init = function(){
        if(!initials){
            $("#btnAddNewClientContactSave").click(function (){
                var newData = {
                    ContactID:  conID,
                    ClientID:   cID,
                    Salutation: $("#selSalutation option:selected").val(),
                    Name:       $("#txtContactName").val(),
                    Position: 	$("#txtCompanyPosition").val(),
                    Email1: 	$("#txtEmailContact1").val(),
                    Email2: 	$("#txtEmailContact2").val(),
                    Phone1: 	$("#txtPhoneContact1").val(),
                    Phone2: 	$("#txtPhoneContact2").val(),
                    Phone3: 	$("#txtPhoneContact3").val(),
                    Phone4: 	$("#txtPhoneContact4").val(),
                    Fax:	 	$("#txtFaxContact").val(),
                    Note:	 	$("#txtContactNote").val()
                } ;
                if(conID === 0){

                    dataBase.insertClientContact(newData,function (){
                        contacts.loadContacts(cID);
                        addnewcontact.resetContact();
                        $.mobile.changePage("#pnContacts");
                    });
                }else{
                    dataBase.editClientContact(newData, function (){
                        contacts.loadContacts(cID);
                        addnewcontact.resetContact();
                        $.mobile.changePage("#pnContacts");
                    });
                }
            });
        }
        initials = true;
    };
    return e;

}(addnewcontact || {}));
